package com.rocpenghua.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.rocpenghua.service.AIService;
import com.rocpenghua.service.DeepSeekAIService;
import org.jetbrains.annotations.NotNull;


public class GenerateCommentsAction extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        final Project project = e.getProject();
        final Editor editor = e.getData(CommonDataKeys.EDITOR);

        if (editor == null || project == null) {
            return;
        }

        String selectedText = editor.getSelectionModel().getSelectedText();
        if (selectedText == null || selectedText.trim().isEmpty()) {
            Messages.showInfoMessage(project, "Please select some code first.", "No Code Selected");
            return;
        }

        AIService aiService = new DeepSeekAIService();
        String explanation = aiService.generateComments(selectedText);

        Messages.showMessageDialog(
                project,
                explanation,
                "Generate Comment",
                Messages.getInformationIcon()
        );
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
        final Project project = e.getProject();
        final Editor editor = e.getData(CommonDataKeys.EDITOR);
        e.getPresentation().setEnabledAndVisible(
                project != null &&
                        editor != null &&
                        editor.getSelectionModel().hasSelection()
        );
    }
}
